package org.shopen.ai.puremvc.view.common
{
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.puremvc.as3.utilities.statemachine.StateMachine;
	import org.shopen.ai.puremvc.ApplicationConstants;
	import org.shopen.ai.puremvc.view.common.components.AdvControlBar;

	public class AdvControlBarMediator extends Mediator
	{
		public static const NAME : String = "AdvControlBarMediator";
		
		[Bindable]
		protected var id : String = "";
		
		protected function get controlBar() : AdvControlBar
		{
			var result : AdvControlBar = viewComponent as AdvControlBar;
			return result;
		}
		
		public function AdvControlBarMediator(viewComponent:AdvControlBar=null, ident : String = "")
		{
			this.id = ident;
			super(NAME + ident, viewComponent);
		}
		
		override public function getMediatorName():String
		{
			var result : String = AdvControlBarMediator.NAME + id;
			return result;
		}
		
		override public function onRegister():void
		{
			addEventListeners();
		}
		
		protected function addEventListeners() : void
		{
			controlBar.addEventListener(AdvControlBar.LEFT_BUTTON_CLICK, leftClickListener);
			controlBar.addEventListener(AdvControlBar.RIGHT_BUTTON_CLICK, rightClickListener);
		}
		
		private function leftClickListener(e : Event) : void
		{
			sendNotification(ApplicationConstants.NAVIGATION_REQUEST, null, ApplicationConstants.PROJECT_PREV_STEP);
		}
		
		private function rightClickListener(e : Event) : void
		{
			sendNotification(ApplicationConstants.NAVIGATION_REQUEST, null, ApplicationConstants.PROJECT_NEXT_STEP);
		}
		
		override public function listNotificationInterests():Array
		{
			return [StateMachine.CHANGED];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name : String = notification.getName();
			switch (name)
			{
				case StateMachine.CHANGED:
					handleStateChange(notification);
					break;
			}
		}
		
		private function handleStateChange(notification : INotification) : void
		{
			var type : String = notification.getType();
			
			switch (type)
			{
				case ApplicationConstants.STATE_PROJECT:
					controlBar.hideLeftButton();
					controlBar.hideRightButton();
					break;
				case ApplicationConstants.STATE_PROJECT_PREVIEW:
					controlBar.hideLeftButton();
					controlBar.showRightButton("Start Exploring ->");
					break;
				case ApplicationConstants.STATE_PROJECT_SETUP:
					controlBar.showLeftButton("<- To Preview");
					controlBar.showRightButton("Start Searching!");
					break;
				case ApplicationConstants.STATE_PROJECT_SEARCHING:
					controlBar.hideLeftButton();
					controlBar.showRightButton("STOP");
					break;
				case ApplicationConstants.STATE_PROJECT_SEARCH_STOPPED:
					controlBar.showLeftButton("Prev");
					controlBar.hideRightButton();
					break;
				case ApplicationConstants.STATE_PROJECT_STATISTIC:
					controlBar.hideRightButton();
					controlBar.showLeftButton("Try Again!");
					break;
			}
		}
	}
}